package pers.qianyu.month_202012.date_20201219.bit_operation;

/**
 * 477. 汉明距离总和
 * https://leetcode-cn.com/problems/total-hamming-distance/
 *
 * @author mizzle rain
 * @date 2020-12-19 21:23
 */
public class TotalHammingDistance {
    public int totalHammingDistance(int[] nums) {
        int res = 0;
        for (int i = 1; i <= 32; i++) {
            int count0 = 0, count1 = 0;
            for (int j = 0; j < nums.length; j++) {
                int last = nums[j] & 1;
                if (last == 0) {
                    count0++;
                } else {
                    count1++;
                }
                nums[j] = nums[j] >> 1;
            }
            res += count1 * count0;
        }
        return res;
    }
}
